﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace YXShop.Web.admin.templates
{
    public partial class stencil_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("011001001");
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty)
                {
                    string type = ChangeHope.WebPage.PageRequest.GetFormString("Option");
                    switch (type)
                    {
                        case "delfolder":
                            if (YXShop.Common.PromptInfo.Message("011001003") != "ok")
                            {
                                string folderpath = ChangeHope.WebPage.PageRequest.GetFormString("path");
                                delFolder(folderpath);
                            }
                            else
                            {
                                Response.Write("no");
                            }
                            break;
                        case "delFile":
                            if (YXShop.Common.PromptInfo.Message("011001003") != "ok")
                            {
                                string filepath = ChangeHope.WebPage.PageRequest.GetFormString("path");
                                delFile(filepath);
                            }
                            else
                            {
                                Response.Write("no");
                            }
                            break;
                        case "Editfolder":
                            if (YXShop.Common.PromptInfo.Message("011001004") != "ok")
                            {
                                string FolderPath = ChangeHope.WebPage.PageRequest.GetFormString("Path");
                                string OldFolderName = ChangeHope.WebPage.PageRequest.GetFormString("OldFolderName");
                                string NewFolderName = ChangeHope.WebPage.PageRequest.GetFormString("NewFolderName");
                                editFolder(FolderPath, OldFolderName, NewFolderName);
                            }
                            else
                            {
                                Response.Write("no");
                            }
                            break;
                        case "EditFile":
                            if (YXShop.Common.PromptInfo.Message("011001004") != "ok")
                            {
                                string FilePath = ChangeHope.WebPage.PageRequest.GetFormString("Path");
                                string OldFileName = ChangeHope.WebPage.PageRequest.GetFormString("OldFileName");
                                string NewFileName = ChangeHope.WebPage.PageRequest.GetFormString("NewFileName");
                                editFile(FilePath, OldFileName, NewFileName);
                            }
                            else
                            {
                                Response.Write("no");
                            }
                            break;
                    }
                    Response.End();
                    return;
                }
                if (ChangeHope.WebPage.PageRequest.GetQueryString("ReturnValue") != string.Empty && ChangeHope.WebPage.PageRequest.GetQueryString("filepath") != string.Empty)
                {
                    YXShop.Common.PromptInfo.Popedom("011001002", "对不起，您没有权限进行编辑");
                    CreateFolder(ChangeHope.WebPage.PageRequest.GetQueryString("ReturnValue"), ChangeHope.WebPage.PageRequest.GetQueryString("filepath"));
                }
                string name = "templates";
                if (ChangeHope.WebPage.PageRequest.GetQueryString("name") != string.Empty)
                {
                    name = ChangeHope.WebPage.PageRequest.GetQueryString("name");
                }

                Get(name, ChangeHope.WebPage.PageRequest.GetQueryString("templateId"));
            }
        }

        #region 显示文件目录
        /// <summary>
        /// 显示文件目录
        /// </summary>
        public void Get(string name,string StyleId)
        {
            string str_lujing = "~\\" + name;
            if (name == "templates")
            {
                this.hlback.Visible = false;
            }
            this.lbpath.Text = name;
            this.hfpath.Value = name;
            DirectoryInfo[] ChildDirectory;                         //子目录集
            FileInfo[] NewFileInfo;                                //当前所有文件
            DirectoryInfo FatherDirectory = new DirectoryInfo(Server.MapPath(str_lujing)); //当前目录
            ChildDirectory = FatherDirectory.GetDirectories("*.*"); //得到子目录集
            NewFileInfo = FatherDirectory.GetFiles();               //得到文件集，可以进行操作
            string str_html = "<table width=\"100%\" id=\"tablist\"  cellspacing=\"0\" cellpadding=\"0\" border=\"1\" bordercolorlight=\"#DDDDDD\""
            + "class=\"datatable\" bordercolordark=\"#FFFFFF\"><tr class='TrTitle' bgcolor='#78B9E6' style='color: White' height='25'><th width='25%'>文件名称</th><th width='10%'>类型</th><th width='10%'>大小</th>"
            + "<th width='20%'>最后修改时间</th><th width='35%'>操作</th></tr>";

            foreach (DirectoryInfo di in ChildDirectory)//显示文件夹
            {
                string strH = "<tr height='25'><td><img src='../images/img/folder.gif' /><a href='stencil_list.aspx?templateId=" + StyleId + "&name=" + name + "/" + di.Name + "'>" + di.Name
                    + "</a></td><td>文件夹</td><td  align='center'>...</td><td  align='center'>" + di.LastWriteTimeUtc + "</td><td><a href=\"javascript:EditFolder('" + di.Name + "');\">改名</a>&nbsp;<a href=\"javascript:DelFolder('" + (name + "/" + di.Name).Replace("/", "\\\\") + "')\">删除</a></td></tr>";
                str_html += strH;
            }
            foreach (FileInfo fi in NewFileInfo)//显示文件
            {
                if (fi.Name.Contains(".htm") || fi.Name.Contains(".html") || fi.Name.Contains(".Shtml"))
                {
                    string strh1 = "<tr height='25'><td><img src='../images/img/html.gif' />" + fi.Name + "</td><td>" + fi.Extension + "文件</td><td  align='center'>" + fi.Length + "字节</td><td  align='center'>"
                    + fi.LastWriteTimeUtc + "</td><td><a href='stencil_edit.aspx?dir="
                    + str_lujing + "&filename=" + fi.Name + "'>编辑模板</a>&nbsp;<a href='amendlable.aspx?styleId="+StyleId+"&dir=" + str_lujing + "&filename=" + fi.Name + "'>更新标签</a>&nbsp;<a href='../../" + str_lujing.Replace("~\\","") + "/" + fi.Name + "' target='_blank'>预览</a>&nbsp;<a href=\"javascript:EditFile('" + fi.Name + "');\">改名</a>&nbsp;<a href=\"javascript:DelFile('" + (name + "/" + fi.Name).Replace("/", "\\\\") + "')\">删除</a></td></tr>";
                    str_html += strh1;
                }
                if (fi.Name.Contains(".css"))
                {
                    string strh1 = "<tr height='25'><td><img src='../images/img//html.gif' />" + fi.Name + "</td><td>" + fi.Extension + "文件</td><td width='10%'  align='center'>" + fi.Length + "字节</td><td  align='center'>"
                                        + fi.LastWriteTimeUtc + "</td><td><a href='stencil_edittext.aspx?dir="
                    + str_lujing + "&filename=" + fi.Name + "'>编辑</a>&nbsp;<a href=\"javascript:EditFile('" + fi.Name + "');\">改名</a>&nbsp;<a href=\"javascript:DelFile('" + (name + "/" + fi.Name).Replace("/", "\\\\") + "')\">删除</a></td></tr>";
                    str_html += strh1;
                }
            }
            str_html += "</table>";
            this.Literal1.Text = str_html;
        }
        #endregion

        #region 创建目录
        private void CreateFolder(string foldername, string folderpath)
        {
            ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
            if (fh.CreateDirectory(Server.MapPath("~\\" + folderpath + "\\" + foldername)))
            {
                Response.Redirect("stencil_list.aspx?name=" + folderpath);
            }
        }
        #endregion

        #region 删除文件夹
        protected void delFolder(string path)
        {
            ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
            if (fh.DeleteDirectory(Server.MapPath("~\\" + path)))
            {
                Response.Write("ok");
            }
            else
            {
                Response.Write("删除失败！");
            }
        }
        #endregion

        #region 删除文件
        protected void delFile(string path)
        {
            ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
            if (fh.DeleteFile(Server.MapPath("~\\" + path)))
            {
                Response.Write("ok");
            }
            else
            {
                Response.Write("删除失败！");
            }
        }
        #endregion

        #region 更改文件夹名
        protected void editFolder(string Path,string OldFolderName,string NewFolderName)
        {
            ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
            if (fh.ReNameFile(Server.MapPath("~\\" + Path), OldFolderName, NewFolderName,0))
            {
                Response.Write("ok");
            }
            else
            {
                Response.Write("更名失败！");
            }
        }
        #endregion

        #region 更改文件名
        protected void editFile(string Path, string OldFileName, string NewFileName)
        {
            ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
            if (fh.ReNameFile(Server.MapPath("~\\" + Path), OldFileName, NewFileName, 1))
            {
                Response.Write("ok");
            }
            else
            {
                Response.Write("更名失败！");
            }
        }
        #endregion
    }
}
